package kr.hkit.summary_java.serviceui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.Department;

public class Panel_DepartmentUITest extends JPanel implements ActionListener {
	private JButton btnDy;
	private String btnText;

	private DaoInterface<Department> dao;
	private InputPanel DeptNo;
	private InputPanel DeptName;
	private InputPanel DeptFloor;
	private JPanel MainPanel;
	private JPanel panel;


	public Panel_DepartmentUITest(String txt, DaoInterface<Department> dao) {
		super();
		this.btnText = txt;
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		MainPanel = new JPanel();
		add(MainPanel, BorderLayout.CENTER);
		MainPanel.setLayout(new GridLayout(0, 1, 0, 0));

		DeptNo = new InputPanel("부서번호");
		MainPanel.add(DeptNo);
		DeptNo.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				// deptno textfield에 있는 text를 int형으로 no에 넣는다.
				int no = Integer.parseInt(DeptNo.getTf().getText().trim());
				Department d = new Department();
				d.setDeptNo(no);// no 를 deptno에 넣어준다.

				try {
					d = dao.searchItem(d);
					DeptName.setTfTxt(d.getDeptName());
					DeptFloor.setTfTxt(d.getFloor() + "");
					if (!btnDy.getText().equals("검색")) {
						panelEnable();
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnDy.getText().equals("추가")) {
						JOptionPane.showMessageDialog(
								Panel_DepartmentUITest.this, "해당부서가 없습니다.");
					} else {
						JOptionPane.showMessageDialog(
								Panel_DepartmentUITest.this, "추가 가능");
					}
				}
			}

		});

		DeptName = new InputPanel("부서명");
		MainPanel.add(DeptName);

		DeptFloor = new InputPanel("위치");
		MainPanel.add(DeptFloor);

		panel = new JPanel();
		add(panel, BorderLayout.SOUTH);

		if (!btnText.equals("추가")) {
			panelDisable();
		}
		btnDy = new JButton(btnText);
		if (btnText.equals("검색")) {
			panelDisable();
		}

		btnDy.addActionListener(this);
		panel.add(btnDy);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if (e.getActionCommand().equals("삭제")) {
			actionPerformedBtnDel(e);
		} else if (e.getActionCommand().equals("수정")) {
			actionPerformedBtnUpdate(e);
		} else if (e.getActionCommand().equals("검색")) {
			actionPerformedBtnSearch(e);
		}
	}

	private void actionPerformedBtnSearch(ActionEvent e) {
		// 검색하기 위해서 deptno textfield에 입력한 값을 int형으로 가져와서 변수no에 담는다.
		int no = Integer.parseInt(DeptNo.getTf().getText().trim());
		Department d = new Department();
		d.setDeptNo(no); // int형으로 가져온 no를 d의 DeptNo에 넣는다.
		try {
			d = dao.searchItem(d);
			DeptName.setTfTxt(d.getDeptName()); // d.deptname을 가져와서 textfield에
												// 넣어준다.
			DeptFloor.setTfTxt(d.getFloor() + ""); // floor 가 인트형이므로 +"" 로 문자로
													// 인식하게 함
			panelEnable(); // 검색이 되면 검색하기 위해서 입력한 DeptNo는 비활성화 하고 나머지 Name과
							// floor는 활성화 시켜준다.
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1) {
			JOptionPane.showMessageDialog(this, "존재하지 않음");
			DeptNo.setTfTxt("");// 존재하지 않는 값을 출력했으므로 textfield를 초기화 시켜주기 위해 입력된
								// 값 DeptNo만 초기화를 해준다.
		}
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		Department d = getDept();
		try {
			dao.updateItemByNo(d);
			JOptionPane.showMessageDialog(this, "수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정실패 다시 확인하세요.");
		}

	}

	private void actionPerformedBtnDel(ActionEvent e) {
		Department d = getDept();
		try {
			dao.deleteItemByNo(d);
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제되었습니다");
			panelDisable();
			panelClear();
			e1.printStackTrace();
		}

	}

	protected void actionPerformedBtnAdd(ActionEvent e) {// 추가 버튼에 대한 Action
		Department d = getDept(); // getDept()는 textfield에 있는 값을 받아서 가져오는 역할
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this, "입력되었습니다");
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}

	}

	private void panelEnable() {
		DeptNo.getTf().setEnabled(false);
		DeptName.getTf().setEnabled(true);
		DeptFloor.getTf().setEnabled(true);
	}

	private void panelDisable() {
		DeptNo.getTf().setEnabled(true);
		DeptName.getTf().setEnabled(false);
		DeptFloor.getTf().setEnabled(false);
	}

	private void panelClear() {
		DeptNo.setTfTxt("");
		DeptName.setTfTxt("");
		DeptFloor.setTfTxt("");
	}

	private Department getDept() {
		int dno = Integer.parseInt(DeptNo.getTf().getText().trim());
		String name = DeptName.getTf().getText();
		int floor = Integer.parseInt(DeptFloor.getTf().getText());
		return new Department(dno, name, floor);
	}

}
